package com.wangtao.firstapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.MyAttentionAdapter;
import com.wangtao.firstapp.inter.OnRVItemClickListener;
import com.wangtao.firstapp.inter.OnRVItemLongClickListener;
import com.wangtao.firstapp.modles.MyAttentionData;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 我的关注页面
 * Created by lanouhn on 16/9/21.
 */
public class MyAttentionActivity extends AppCompatActivity implements OnRVItemClickListener, OnRVItemLongClickListener {
    private ImageView iv_back_titlebar_myattention;// 标题栏返回按钮
    private RecyclerView recyclerview_myattention;
    private MyAttentionData myAttentionData;// 我的关注实体类
    private MyAttentionAdapter myAttentionAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myattention);
        initVew();
        initEvent();
        initData();
    }

    private void initVew() {
        iv_back_titlebar_myattention = (ImageView) findViewById(R.id.iv_back_titlebar_myattention);
        recyclerview_myattention = (RecyclerView) findViewById(R.id.recyclerview_myattention);
    }

    private void initEvent() {
        iv_back_titlebar_myattention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {

        String url = "http://open4.bantangapp.com/users/fllow/listAttention";
        String param = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=sougou&app_installtime=1471923362&app_versions=5.8.8&os_versions=5.1&screensize=1080&v=19&page=0&pagesize=10&visit_user_id=2559956";
        try {
            HttpUtils.doPostAsyn(url, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    myAttentionData = gson.fromJson(result, MyAttentionData.class);
                    if (myAttentionData.getData().getList().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    LinearLayoutManager llm = new LinearLayoutManager(MyAttentionActivity.this);
                    recyclerview_myattention.setLayoutManager(llm);
                    myAttentionAdapter = new MyAttentionAdapter(MyAttentionActivity.this, myAttentionData);
                    recyclerview_myattention.setAdapter(myAttentionAdapter);
                    myAttentionAdapter.setClickListener(MyAttentionActivity.this);
                    myAttentionAdapter.setLongClickListener(MyAttentionActivity.this);
                    break;
            }
        }
    };

    @Override
    public void OnRVItemClickListener(int position) {
        // Toast.makeText(this, myAttentionData.getData().getList().get(position).getNickname(), Toast.LENGTH_SHORT).show();
        String userid = myAttentionData.getData().getList().get(position).getUser_id();
        Intent intent = new Intent(MyAttentionActivity.this,OriginalAuthorActivity.class);
        intent.putExtra("userid",userid);
        startActivity(intent);
    }

    @Override
    public void OnRVItemLongClickListener(int position) {
        // Toast.makeText(this, myAttentionData.getData().getList().get(position).getNickname(), Toast.LENGTH_SHORT).show();
        String userid = myAttentionData.getData().getList().get(position).getUser_id();
        Intent intent = new Intent(MyAttentionActivity.this,OriginalAuthorActivity.class);
        intent.putExtra("userid",userid);
        startActivity(intent);
    }
}
